<?php
namespace Home\Controller;
use Home\Controller\UserController;
use Common\Api\SendPhoneCodeApi;
class WithdrawController extends UserController {
public function _initialize(){
	parent::_initialize();
	}	
	public function sendPhone(){
		$code_name = 'tixian';
		$SENDPHONE = new SendPhoneCodeApi($this->user['phone']);
		$send = $SENDPHONE->send($code_name);
		if($send == 1){
			$data_return['status'] = 1;
			$data_return['info']   = '发送成功';
			return $this->ajaxReturn($data_return);
		}else{
			$data_return['status'] = -1;
			$data_return['info']   = $send;
			return $this->ajaxReturn($data_return);
		}
	}
	public function withdraw(){
		$currency_id=I('currency_id');
    	$num=I('num');
    	$pwd=I('pwd');
    	$address=I('address');
     	$code=I('code');
	    if(empty($code)){
	    	$data['status']=0;
	    	$data['info'] = "请填写验证码";
	    	$this->ajaxReturn($data);
	    }
		if($code != session('codetixian'.$this->user['phone'])){
			$data_return['status'] = -13;
			$data_return['info']   = '手机验证码有误';
			$this->ajaxReturn($data_return);
		}
    	if(empty($num)){
    		$data['status']=0;
    		$data['info'] = "请填写提币数量";
    		$this->ajaxReturn($data);
    	}
    	if(empty($pwd)){
    		$data['status']=0;
    		$data['info'] = "请填写交易密码";
    		$this->ajaxReturn($data);
    	}
    	if(empty($address)){
    		$data['status']=0;
    		$data['info'] = "请填写提币地址";
    		$this->ajaxReturn($data);
    	}
    	if(md5($pwd)!=$this->user['pwdtrade']){
    		$data['status']=0;
    		$data['info'] = "交易密码不正确";
    		$this->ajaxReturn($data);
    	}
    	$cu=M('member_money')->where('uid ='.$this->uid.' and currency_id ='.$currency_id)->find();
    	if($num>$cu['num']){
    		$data['status']=0;
    		$data['info'] = "钱包余额不足";
    		$this->ajaxReturn($data);
    	}
    	$arr['uid']=$this->uid;
    	$arr['num']=$num;
    	$arr['currency_id']=$currency_id;
    	$arr['act_num']=$num*(1-$this->config['withdraw_fee']);
    	$arr['address']=$address;
    	$arr['add_time']=time();
    	$r[]=M('withdraw')->add($arr);
    	$r[]=M('member_money')->where('uid ='.$this->uid.' and currency_id ='.$currency_id)->setDec('num',$num);
    	$r[]=$this->addFinance($this->uid, 2, '提现', $num, 1,$currency_id);
    	if(in_array(false, $r)){
    		$back['status']=-1;
    		$back['info']='服务器繁忙';
    		$this->ajaxReturn($back);
    	}else{
    		$back['status']=1;
    		$back['info']='提交成功';
    		$this->ajaxReturn($back);
    	}
    }
}